package com.yoda.controller;

import com.yoda.constant.MessageConstant;
import com.yoda.result.Result;
import com.yoda.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping("/common/upload")
    public Result upload(@RequestParam MultipartFile file){
        //获取原始文件名
        String originalFilename = file.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));

        String fileName= UUID.randomUUID().toString().replace("-","")+substring;


        //调用工具
        try {
            aliOssUtil.upload(file.getBytes(), fileName);
            Map<String,Object>map =new HashMap<>();
            map.put("mime","image/jpeg");
            map.put("name","");
            map.put("savePath","\\"+fileName);
            map.put("size",111);
            return Result.success(map);
        } catch (IOException e) {
            return Result.error(e.getMessage());
        }
    }

}
